﻿/********************************************************************************/
// <copyright file="StringExtention.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="11/12/2012 2:16:35 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Model.Extensions
{
    using System.IO;
    using System.Text.RegularExpressions;

    public static class StringExtention
    {
        private static readonly Regex _regex;
        public static string GetPlainTextWords(this string content, int words)
        {
            string result = Regex.Replace(content, @"<(.|\n)*?>", string.Empty).Replace(@"\r\t", "");
            int i = 0;
            int pos = result.IndexOf(" ", 0);
            var previousPos = pos;
            while (i < words && pos >= 0)
            {
                i++;
                previousPos = pos;
                pos = result.IndexOf(" ", pos + 1);
            }
            if (i < words)
            {
                return content;
            }
            else
            {
                return result.Substring(0, previousPos) + "...";
            }
        }

        public static bool IsPicturePath(this string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return false;
            }
            else
            {
                string extension = Path.GetExtension(path).ToLower();
                return ".jpg.png.bmp.tif.gif".Contains(extension);
            }
        }
    }
}
